<?php
/**
 * Created by PhpStorm.
 * User: 盛茂国
 * Tel: 18394333958
 * Date: 2020/6/2
 * Time: 10:31
 */
namespace app\api\controller;

use app\common\controller\Api;

use think\Request;

class Store extends Api
{
    protected $noNeedLogin = ['index','detail'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $pageNum=($request->param('pageNum')) ? $request->param('pageNum') : 5;
        $page=($request->param('page')) ? $request->param('page','intval') :0;

        $store=new \app\admin\model\Store();
        $list=collection($store->with('storeinfo')->order('storeid desc')->page($page,$pageNum)->select())->toArray();

         foreach ($list as $key=>$value){
             $list[$key]['storeimages']=$request->domain().$value['storeimages'];
             $list[$key]['identimages']=$request->domain().$value['identimages'];
             $images=explode(',',$value['images']);

             $arr=[];
             foreach ($images as $k=>$v){
                 if($k<3){
                     $arr[$k]=$request->domain().$v;
                 }else{
                     break;
                 }
             }
             $list[$key]['images']=$arr;
         }
         //print_r($list);die;
        $data['total']=db('store')->count();
        $data['page']=$page;
        $data['pageNum']=$pageNum;
        $data['data']=$list;
        return json(['status'=>200,'msg'=>'获取门店列表成功','data'=>$data]);
    }

    public function detail(Request $request){
        $pageNum=($request->param('pageNum')) ? $request->param('pageNum') : 5;
        $page=($request->param('page')) ? $request->param('page','intval') :0;
        $storeid=$request->param('storeid');
        if($request->isPost()){
            $store=new \app\admin\model\Store();
            $list=$store->with('Storeinfo')->with('Comments')->where('store.storeid','=',$storeid)->find();
            if($list){
                $listarr=$list->toArray();
                unset($listarr['storeinfo']);
                $listarr['storeimages']=$request->domain().$listarr['storeimages'];
                $listarr['identimages']=$request->domain().$listarr['identimages'];

                //处理门店详情的多图
                if(!empty($listarr['images'])){ //如果详情图不空
                    $images=explode(',',$listarr['images']);
                    if(is_array($images) && count($images) > 1){//多图
                        $imagesarr=[];
                        foreach ($images as $k=>$v){
                            $imagesarr[$k]= $request->domain().$v;
                        }
                        $listarr['images']=$imagesarr;
                    }else{//单图
                        $listarr['images']=[$request->domain().$listarr['images']];
                    }
                }

                //处理评论的绝对url
                foreach ($listarr['comments'] as $key=>$value){
                    $strsrc=$value['imageData'];
                    if(empty($strsrc)){
                        $listarr['comments'][$key]['imageData']=$value['imageData'];
                    }else{
                        $strsrc=trim($strsrc,'[');
                        $strsrc=trim($strsrc,']');
                        $strsrc=str_replace('\\','',$strsrc);
                        $imgarr=explode(',',$strsrc);
                        if(is_array($imgarr) && count($imgarr) > 1){//多图
                            foreach ($imgarr as $k=>$v){
                                $imgarr[$k]= $request->domain().str_replace('"','',$v);
                            }
                            $listarr['comments'][$key]['imageData']=$imgarr;
                        }else{//单图
                            //$listarr['comments'][$key]['imageData']= [$request->domain().str_replace('"','',$imgarr[0])];
                            //print_r(strlen($imgarr[0]));
                            if(strlen($imgarr[0])>8){
                                $listarr['comments'][$key]['imageData']= [$request->domain().str_replace('"','',$imgarr[0])];
                            }else{
                                $listarr['comments'][$key]['imageData']=[];
                            }
                        }
                    }

                    $listarr['comments'][$key]['addtime']=date('Y-m-d H:i:s',$value['addtime']);
                    $user=db('user')->find(['id'=>$value['uid']]);
                    $listarr['comments'][$key]['nickname']=$user['nickname'];
                    $listarr['comments'][$key]['avatar']=$user['avatar'];
                }


                $listarr['comments']=mypage($pageNum,$page,$listarr['comments'],1);

                return json(['status'=>200,'msg'=>'请求成功','data'=>$listarr]);
            }else{
                return json(['status'=>400,'msg'=>'storeid参数错误']);
            }

        }else{
            return json(['status'=>400,'msg'=>'非法请求']);
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
